package leetcode;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2022-03-17 21:24
 **/
public class LeetCode720 {

    public String longestWord(String[] words) {
        Arrays.sort(words, (a, b) -> a.length() != b.length() ? a.length() - b.length() : b.compareTo(a));
        Set<String> set = new HashSet<>();
        set.add("");
        String ans = "";
        for (String word : words) {
            if (set.contains(word.substring(0, word.length() - 1))) {
                set.add(word);
                ans = word;
            }
        }
        return ans;
    }

}
